﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

public class ExcelTools : EditorWindow
{
    /// <summary>
    /// 当前编辑器窗口实例
    /// </summary>
    private static ExcelTools instance;

    /// <summary>
    /// Excel文件列表
    /// </summary>
    private static List<string> excelList;

    /// <summary>
    /// 项目根路径	
    /// </summary>
    private static string pathRoot;

    /// <summary>
    /// 滚动窗口初始位置
    /// </summary>
    private static Vector2 scrollPos;

    /// <summary>
    /// 输出格式索引
    /// </summary>
    private static int indexOfFormat = 0;

    /// <summary>
    /// 输出格式
    /// </summary>
    private static string[] formatOption = new string[] { "JSON", "CSV", "XML", "LUA" };

    /// <summary>
    /// 编码索引
    /// </summary>
    private static int indexOfEncoding = 0;

    /// <summary>
    /// 编码选项
    /// </summary>
    private static string[] encodingOption = new string[] { "UTF-8", "GB2312" };

    /// <summary>
    /// 是否保留原始文件
    /// </summary>
    private static bool keepSource = true;

    /// <summary>
    /// 显示当前窗口	
    /// </summary>
    [MenuItem("Tools/ExcelTools")]
    static void ShowExcelTools()
    {
        Init();
        //加载Excel文件
        LoadExcel();
        instance.Show();
    }

    void OnGUI()
    {
        DrawOptions();
        DrawExport();
    }

    /// <summary>
    /// 绘制插件界面配置项
    /// </summary>
    private void DrawOptions()
    {
        GUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("请选择格式类型:", GUILayout.Width(85));
        indexOfFormat = EditorGUILayout.Popup(indexOfFormat, formatOption, GUILayout.Width(125));
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("请选择编码类型:", GUILayout.Width(85));
        indexOfEncoding = EditorGUILayout.Popup(indexOfEncoding, encodingOption, GUILayout.Width(125));
        GUILayout.EndHorizontal();

        keepSource = GUILayout.Toggle(keepSource, "保留Excel源文件");

        if (GUILayout.Button("生成Json"))
        {
            ConvertToJson();
        }
        if (GUILayout.Button("生成脚本"))
        {
            CreatScript();
        }
        if (GUILayout.Button("检查"))
        {
            Check();
        }
    }

    /// <summary>
    /// 绘制插件界面输出项
    /// </summary>
    private void DrawExport()
    {
        if (excelList == null) return;
        if (excelList.Count < 1)
        {
            EditorGUILayout.LabelField("目前没有Excel文件被选中哦!");
        }
        else
        {
            EditorGUILayout.LabelField("下列项目将被转换为" + formatOption[indexOfFormat] + ":");
            GUILayout.BeginVertical();
            scrollPos = GUILayout.BeginScrollView(scrollPos, false, true, GUILayout.Height(150));
            foreach (string s in excelList)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Toggle(true, s);
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();

            //输出
            if (GUILayout.Button("转换"))
            {
                Convert();
            }
        }
    }
    class JsonTmp
    {
        public string 枚举;
    }
    private void CreatScript()
    {

        List<string> dirs = new List<string>(Directory.EnumerateFiles(Application.streamingAssetsPath + $"/Json", "*.json", SearchOption.AllDirectories));
        List<string> createdFiles = new List<string>();
        List<string> filesFullName = new List<string>();
        foreach (var dir in dirs)
        {
            string fileName = dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1);
            if (!createdFiles.Contains(fileName))
            {
                Debug.Log($"{fileName}     {dir}");
                createdFiles.Add(fileName);
                filesFullName.Add(dir);
            }

            //string excelPath = dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1);
            //ExcelUtility excel = new ExcelUtility(excelPath);
            //excel.ConvertToJson(excelPath, Encoding.GetEncoding("utf-8"));
        }
        string itemTypeStr = "";

        string soundTypeStr = "";

        for (int i = 0; i < filesFullName.Count; i++)
        {
            string file = File.ReadAllText(filesFullName[i]);
            JsonTmp[] tmps = EFramework.Utility.JSONParser.FromJson<JsonTmp[]>(file);
            if (createdFiles[i][2] == '5' || createdFiles[i][2] == '6')
            {
                foreach (var tmp in tmps)
                    soundTypeStr += tmp.枚举 + "\n";
            }
            else
            {
                foreach (var tmp in tmps)
                    itemTypeStr += tmp.枚举 + "\n";
            }
        }

        string soundTypeTemplate = $"public enum SoundType\n{{\nNone=0,\n{soundTypeStr.Replace("（", "_").Replace("）", "").Replace("“", "_").Replace("”", "").Replace(" ", "")}}}";
        string itemtypeTemplate = $"public enum ItemType\n{{\nNone=0,\n{itemTypeStr.Replace("（", "_").Replace("）", "").Replace("“", "_").Replace("”", "").Replace(" ", "")}}}";

        File.WriteAllText("E:/Project/Lost_in_Abyss2/Assets/Gameplay/MJ2/Interaction/Item/ItemType.cs", itemtypeTemplate);
        File.WriteAllText("E:/Project/Lost_in_Abyss2/Assets/Gameplay/MJ2/Interaction/Item/SoundType.cs", soundTypeTemplate);
        Debug.Log("脚本已生成");
        AssetDatabase.Refresh();
    }
    private void ConvertToJson()
    {
        List<string> dirs = new List<string>(Directory.EnumerateFiles(Application.streamingAssetsPath + $"\\Excel", "*.xlsx", SearchOption.AllDirectories));

        foreach (var dir in dirs)
        {
            Debug.Log(dir);
            //string excelPath = Application.streamingAssetsPath + $"/Excel/" + dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1);
            ExcelUtility excel = new ExcelUtility(dir);
            excel.ConvertToJson(dir, Encoding.GetEncoding("utf-8"));
        }
        AssetDatabase.Refresh();
        //instance.Close();
    }
    private void Check()
    {

    }
    /// <summary>
    /// 转换Excel文件
    /// </summary>
    private static void Convert()
    {
        foreach (string assetsPath in excelList)
        {
            //获取Excel文件的绝对路径
            string excelPath = pathRoot + "/" + assetsPath;
            //构造Excel工具类
            ExcelUtility excel = new ExcelUtility(excelPath);

            //判断编码类型
            Encoding encoding = null;
            if (indexOfEncoding == 0 || indexOfEncoding == 3)
            {
                encoding = Encoding.GetEncoding("utf-8");
            }
            else if (indexOfEncoding == 1)
            {
                encoding = Encoding.GetEncoding("gb2312");
            }

            //判断输出类型
            string output = "";
            if (indexOfFormat == 0)
            {
                output = excelPath.Replace(".xlsx", ".json");
                //excel.ConvertToJson(output, encoding);
            }
            else if (indexOfFormat == 1)
            {
                output = excelPath.Replace(".xlsx", ".csv");
                excel.ConvertToCSV(output, encoding);
            }
            else if (indexOfFormat == 2)
            {
                output = excelPath.Replace(".xlsx", ".xml");
                excel.ConvertToXml(output);
            }
            else if (indexOfFormat == 3)
            {
                output = excelPath.Replace(".xlsx", ".lua");
                excel.ConvertToLua(output, encoding);
            }

            //判断是否保留源文件
            if (!keepSource)
            {
                FileUtil.DeleteFileOrDirectory(excelPath);
            }

            //刷新本地资源
            AssetDatabase.Refresh();
        }

        //转换完后关闭插件
        //这样做是为了解决窗口
        //再次点击时路径错误的Bug
        instance.Close();

    }

    /// <summary>
    /// 加载Excel
    /// </summary>
    private static void LoadExcel()
    {
        if (excelList == null) excelList = new List<string>();
        excelList.Clear();
        //获取选中的对象
        object[] selection = (object[])Selection.objects;
        //判断是否有对象被选中
        if (selection.Length == 0)
            return;
        //遍历每一个对象判断不是Excel文件
        foreach (Object obj in selection)
        {
            string objPath = AssetDatabase.GetAssetPath(obj);
            if (objPath.EndsWith(".xlsx"))
            {
                excelList.Add(objPath);
            }
        }
    }

    private static void Init()
    {
        //获取当前实例
        instance = EditorWindow.GetWindow<ExcelTools>();
        //初始化
        pathRoot = Application.dataPath;
        //注意这里需要对路径进行处理
        //目的是去除Assets这部分字符以获取项目目录
        //我表示Windows的/符号一直没有搞懂
        pathRoot = pathRoot.Substring(0, pathRoot.LastIndexOf("/"));
        excelList = new List<string>();
        scrollPos = new Vector2(instance.position.x, instance.position.y + 75);
    }

    void OnSelectionChange()
    {
        //当选择发生变化时重绘窗体
        Show();
        LoadExcel();
        Repaint();
    }
}
